/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.*;

/**
 * Generates a random matrix for test purposes.
 *
 * @author Corvus
 */
public class RandomMatrix {

    /**
     * Returns a random matrix of a wanted size.
     *
     * @param rows Amount of rows in the matrix.
     * @param columns Amount of columns in the matrix.
     * @return An int[][] format matrix
     */
    public static double[][] randomMatrix(int rows, int columns) {
        Random r = new Random(1);
        double[][] matrix = new double[rows][columns];
        for (int y = 0; y < matrix.length; y++) {
            for (int x = 0; x < matrix[y].length; x++) {
                matrix[y][x] = (r.nextDouble()-0.5)*10;
            }
        }

        return matrix;
    }

    /**
     * Creates a 500,500 and 500,1000 matrices and writes them as
     * random_matrix_a and random_matrix_b as both .xml and .bin files.
     * 
     * @param args
     */
    public static void main(String[] args) {
    	double[][] matrixA = randomMatrix(1000, 1000);
    	double[][] matrixB = randomMatrix(1000, 1000);

        try {
            long start = System.nanoTime();
            MatrixWriter.writeBinary(matrixA, "random_matrix_a.bin");
            MatrixWriter.writeBinary(matrixB, "random_matrix_b.bin");
            long end = System.nanoTime();

            System.out.println("Binary write time: " + (end - start) / 1000000 + "ms");

            start = System.nanoTime();
            MatrixWriter.writeXML(matrixA, "random_matrix_a.xml");
            MatrixWriter.writeXML(matrixB, "random_matrix_b.xml");
            end = System.nanoTime();

            System.out.println("XML write time: " + (end - start) / 1000000 + "ms");

        } catch (IOException ex) {
            System.err.println("Matrix cannot be written.");
        }


        System.out.println("Matrices ready.");
        System.out.println("Target matrix size: " + matrixB[0].length + " x " + matrixA.length
                + " = " + matrixB[0].length * matrixA.length);

    }
}
